var $elm={
	clear:function(elm){
		while (elm.firstChild) {
			elm.removeChild(elm.firstChild);
		}
		return elm;
	},

	attr:function(elm,key,val){
		if(val){
			return elm.setAttribute(key,val);
		}else{
			return elm.getAttribute(key);
		}
	},

	append:function(elm,tag,attr,val){
		var newElm=elm.appendChild(document.createElement(tag));
		if(typeof(attr)=="object"){
			for (var key in attr){
				newElm.setAttribute(key,attr[key]);
			}
		}
		if(val){
			newElm.textContent=val;
		}
		return newElm;
	},

	get:function(key,parent){
		if(parent){
		
		}else{
			var parent=document;
		}
		
		if(typeof(key)=="object"){
			var obj=key;
			for (var key in obj){
				if(obj[key]){
					var val=obj[key];
				}else{
					var val='*';
				}
				return parent.getElementsByAttribute(key,val);
			}
		}
		if (key.match(/^#/)){
			return parent.getElementById(key.replace(/^#/,''));
		}
		if (key.match(/^\./)){
			return parent.getElementsByClassName(key.replace(/^\./,''));
		}
		return parent.getElementsByTagName(key);
	},

	xul:function (elm,val){
		if (val==undefined){
			return this.getXML(elm);
		//	return temp;
		}else{
			var dom=this.parser(val);
			if(!dom){return;}
			while(elm.firstChild){
				elm.removeChild(elm.firstChild);
			}
			elm.appendChild(dom);
		}
	},
	parser:function(str){
		var ns=" xmlns = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul' ";
		var a=str.split(">");
		
		var str2='';
		for (var i=0;i<=a.length;i++){
			if(!a[i]){continue}
			var temp=a[i]+'>';
	//		this.log(temp)
			if(a[i].match(/\<\//)){str2+=temp;continue}
			if(temp.match(/\/\>/)){
				var s= temp.replace(/(\/)\>/g,ns+'/>');
			}else if(str.match(/\>/)){
				var s= temp.replace(/\>/g,ns+'>');
			}
			str2+=s;
		}
		var str=str2;
		var dom = document.importNode((new DOMParser()).parseFromString(str ,"text/xml").documentElement.cloneNode(true),true);
		if(dom.nodeName == "parsererror"){
			return false;
		}
		return dom;
	},

	getXML:function(elm){
		function toXML(elt){
	        if (elt.nodeType == 1){
	            xml.push('<', elt.localName.toLowerCase());

	            for (var i = 0; i < elt.attributes.length; ++i){
	                var attr = elt.attributes[i];

	                // Hide attributes set by Firebug
	                if (attr.localName.indexOf("firebug-") == 0)
	                    continue;

	                xml.push(' ', attr.localName, '=', escapeHTMLAttribute(attr.nodeValue));
	            }

	            if (elt.firstChild){
	                xml.push('>');

	                for (var child = elt.firstChild; child; child = child.nextSibling)
	                    toXML(child);

	                xml.push('</', elt.localName.toLowerCase(), '>');
	            }else{
	                xml.push('/>');
				}
	        }else if (elt.nodeType == 3){
	            xml.push(elt.nodeValue);
	        }else if (elt.nodeType == 4){
	            xml.push('<![CDATA[', elt.nodeValue, ']]>');
	        }else if (elt.nodeType == 8){
	            xml.push('<!--', elt.nodeValue, '-->');
			}
		}

		var xml = [];
		var old=elm.cloneNode(true);
		var temp='';
		while(old.firstChild){
				//	temp+=(new XMLSerializer()).serializeToString(old.firstChild);
			toXML(old.firstChild);
			old.removeChild(old.firstChild);
		}
		return xml.join("");
	},
	log: function(str) {
		Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService).logStringMessage(str);
	},
};

function escapeHTMLAttribute(value){
    function replaceChars(ch) {
        switch (ch){
            case "&":
                return "&amp;";
            case "'":
                return apos;
            case '"':
                return quot;
        }
        return "?";
    };
    var apos = "&#39;", quot = "&quot;", around = '"';
    if( value.indexOf('"') == -1 ) {
        quot = '"';
        apos = "'";
    } else if( value.indexOf("'") == -1 ) {
        quot = '"';
        around = "'";
    }
    return around + (String(value).replace(/[&'"]/g, replaceChars)) + around;
}